package com.byzk.p2p.admin.money.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.base.util.BaseUtil;
import com.byzk.p2p.admin.base.util.DateUtils;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.borrow.dao.BorrowDao;
import com.byzk.p2p.admin.borrow.dao.ChangeDao;
import com.byzk.p2p.admin.borrow.dao.TenderDao;
import com.byzk.p2p.admin.borrow.dto.Borrow;
import com.byzk.p2p.admin.borrow.dto.Change;
import com.byzk.p2p.admin.borrow.dto.UserTender;
import com.byzk.p2p.admin.borrow.util.BorrowStatus;
import com.byzk.p2p.admin.borrow.vo.BorrowRepayExportVo;
import com.byzk.p2p.admin.borrow.vo.BorrowTempVo;
import com.byzk.p2p.admin.fuiou.config.Config;
import com.byzk.p2p.admin.fuiou.dto.TransferToFreezenDto;
import com.byzk.p2p.admin.fuiou.util.SecurityUtils;
import com.byzk.p2p.admin.fuiou.util.WebUtils;
import com.byzk.p2p.admin.fuiou.util.XmlUtils;
import com.byzk.p2p.admin.money.dao.BorrowPaymentDao;
import com.byzk.p2p.admin.money.dao.BorrowPaymentDetailDao;
import com.byzk.p2p.admin.money.dao.FundRecordDao;
import com.byzk.p2p.admin.money.dto.BorrowPayment;
import com.byzk.p2p.admin.money.dto.BorrowPaymentDetail;
import com.byzk.p2p.admin.money.dto.FundRecord;
import com.byzk.p2p.admin.money.util.FundRecordType;
import com.byzk.p2p.admin.money.vo.BorrowPaymentVo;
import com.byzk.p2p.admin.money.vo.SumMoneyVo;
import com.byzk.p2p.admin.system.dao.CallbackDao;
import com.byzk.p2p.admin.system.dao.MemberDao;
import com.byzk.p2p.admin.system.dto.Callback;
import com.byzk.p2p.admin.system.dto.MemberLevel;
import com.byzk.p2p.admin.system.service.SMSService;
import com.byzk.p2p.admin.user.dao.UserAccountDao;
import com.byzk.p2p.admin.user.dao.UserDao;
import com.byzk.p2p.admin.user.dto.User;
import com.byzk.p2p.admin.user.dto.UserAccount;
import com.byzk.p2p.admin.user.service.UserAccountService;
import com.byzk.p2p.admin.user.service.UserService;

/**
 * 借款人还款service
 * @author suker.zhou
 *
 */
@Service
public class BorrowPaymentService {
	
	private static final Logger log = LoggerFactory.getLogger(BorrowPaymentService.class);
	
	@Autowired
	private BorrowPaymentDao borrowPaymentDao;
	
	@Autowired
	private UserAccountDao userAccountDao;

	@Autowired
	private BorrowPaymentDetailDao borrowPaymentDetailDao;
	
	@Autowired
	private TenderDao tenderDao;
	
	@Autowired
	private BorrowDao borrowDao;
	
	@Autowired
	private FundRecordDao fundRecordDao;
	
	@Autowired
	private ChangeDao changeDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private CallbackDao callbackDao;
	
	@Autowired
	private SMSService smsService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserAccountService accountService;
	@Autowired
	private MemberDao memberDao;
	
	
	/**
	 * 借款人还款新增修改
	 * @param fundRecord
	 * @return
	 */
	public Integer saveOrUpdate(BorrowPayment record){
		if(record.getId()==null){
			return borrowPaymentDao.insert(record);
		}else{
			return borrowPaymentDao.update(record);
		}
	}
	
	/**
	 * 借款人还款
	 * @param uid
	 * @param paymentId
	 * @return
	 * @author suker.zhou
	 * @throws Exception
	 */
	public Integer payment(Integer paymentId) throws Exception{
		Double payTotal=0.0,payFund=0.0,payInterest=0.0;
		String status="";
		BorrowPayment payment=borrowPaymentDao.getById(paymentId);
		if("N".equals(payment.getStatus())){
			Borrow borrow=borrowDao.getBorrowById(payment.getBorrowId());
			//待还款总额
			payTotal=payment.getNotPaymentTotal();
			//待还款本金
			payFund=payment.getNotPaymentFund();
			//待还款利息
			payInterest=payment.getNotPaymentInterest();
			
			payment.setStatus("Y");
			payment.setPaymentTotal(payTotal);//已还总额
			payment.setPaymentFund(payFund);//已还本金
			payment.setPaymentInterest(payInterest);//已还利息		
			payment.setNotPaymentTotal(0.0);//待还款总额
			payment.setNotPaymentFund(0.0);//待还款本金
			payment.setNotPaymentInterest(0.0);//待还款利息
			payment.setActualDate(new Date());//实际还款日
			int count=DateUtils.betweenDays(payment.getExpectDate(), payment.getActualDate());
			if(count>0){
				payment.setOverdueDay(count);//逾期天数
			}
			//逾期罚息(暂未设置)
			payment.setOverdueFee(0.0);
			payment.setOverdueInterest(0.0);
			
			List<BorrowPaymentDetail> detailList=borrowPaymentDetailDao.selectByPidAndIndex(payment.getId());
			List<UserTender> tenderList=new ArrayList<UserTender>();
			List<FundRecord> fundList=new ArrayList<FundRecord>();
			for (BorrowPaymentDetail detail : detailList) {
				detail.setActualDate(new Date());
				int temp=DateUtils.betweenDays(detail.getExpectDate(), detail.getActualDate());
				if(temp>0){
					detail.setOverdueDay(count);//逾期天数
				}
				
				Integer uid=null;
				if("Y".equals(detail.getIsChange())){//是否债权转让
					Change change=changeDao.getChangeByTenderIdAndBorrowIdAndUserId(detail.getBorrowId(), detail.getUserId(), detail.getTenderId());
					if(change!=null){
						uid=change.getChangeUserId();
						UserAccount account=userAccountDao.getByUserId(uid);
						
						//资金流水一分为二：一条本金一条利息
						FundRecord fund=new FundRecord();
						fund.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
						fund.setCreateTime(new Date());
						fund.setUserId(uid);
						fund.setType(FundRecordType.Type.REPAY.index);
						fund.setIncome(detail.getNotPaymentFund());
						fund.setExpend(0.0);
						fund.setFreezen(0.0);
						fund.setObjectId(detail.getTenderId());
						fund.setBalance(account.getBalance()+detail.getNotPaymentFund());
						fund.setRemark("收到还款(本金)");
						fund.setTerminal("PC");
						fundList.add(fund);
						
						account.setBalance(account.getBalance()+detail.getNotPaymentFund());
		//					account.setTotalAmount(account.getTotalAmount()+detail.getPaymentFund());
						//更新投资用户账户总收入
						account.setTotalIncome(account.getTotalIncome()+detail.getNotPaymentFund());
						userAccountDao.update(account);
						
						FundRecord fund2=new FundRecord();
						fund2.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
						fund2.setCreateTime(new Date());
						fund2.setUserId(uid);
						fund2.setType(FundRecordType.Type.REPAY.index);
						fund2.setIncome(detail.getNotPaymentInterest());
						fund2.setExpend(0.0);
						fund2.setObjectId(detail.getTenderId());
						fund2.setRemark("收到还款(利息)");
						fund2.setFreezen(0.0);
						fund2.setBalance(account.getBalance()+detail.getNotPaymentInterest());					
						fund2.setTerminal("PC");
						fundList.add(fund2);
		
						account.setBalance(account.getBalance()+detail.getNotPaymentInterest());
		//					account.setTotalAmount(account.getTotalAmount()+detail.getPaymentInterest());
						//更新投资用户账户总收入
						account.setTotalIncome(account.getTotalIncome()+detail.getNotPaymentInterest());
						userAccountDao.update(account);
						
						FundRecord fund1=new FundRecord();
						fund1.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
						fund1.setCreateTime(new Date());
						fund1.setUserId(uid);
						fund1.setType(FundRecordType.Type.FEE.index);
						fund1.setIncome(0.0);
						fund1.setExpend(detail.getFee());
						fund1.setObjectId(detail.getTenderId());
						fund1.setRemark("利息管理费");
						fund1.setTerminal("PC");
						fund1.setFreezen(0.0);
						fund1.setBalance(account.getBalance()-detail.getFee());						
						fundList.add(fund1);
		
						//用户支付利息管理费
						userToPlatform(uid,detail.getFee());
						
						//更新投资用户账户可用余额
						account.setBalance(account.getBalance()-detail.getFee());
						account.setTotalAmount(account.getTotalAmount()-detail.getFee());
						//更新投资用户账户总收入
						account.setTotalIncome(account.getTotalIncome()-detail.getFee());
						userAccountDao.update(account);	
						//发送收到还款短信
						User user = userService.getById(uid);
						
//						smsService.sendPaymentSms(user.getPhone(), BaseUtil.StringToDoubletoFixed2(detail.getNotPaymentFund()+detail.getNotPaymentInterest()-detail.getFee()), uid);
						
					}
				}else{
					uid=detail.getUserId();
					UserAccount account=userAccountDao.getByUserId(uid);
					
					//资金流水一分为二：一条本金一条利息
					FundRecord fund=new FundRecord();
					fund.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
					fund.setCreateTime(new Date());
					fund.setUserId(uid);
					fund.setType(FundRecordType.Type.REPAY.index);
					fund.setIncome(detail.getNotPaymentFund());
					fund.setExpend(0.0);
					fund.setFreezen(0.0);
					fund.setObjectId(detail.getTenderId());
					fund.setBalance(account.getBalance()+detail.getNotPaymentFund());
					fund.setRemark("收到还款(本金)");
					fund.setTerminal("PC");
					fundList.add(fund);
					
					account.setBalance(account.getBalance()+detail.getNotPaymentFund());
		//				account.setTotalAmount(account.getTotalAmount()+detail.getPaymentFund());
					//更新投资用户账户总收入
					account.setTotalIncome(account.getTotalIncome()+detail.getNotPaymentFund());
					userAccountDao.update(account);
					
					FundRecord fund2=new FundRecord();
					fund2.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
					fund2.setCreateTime(new Date());
					fund2.setUserId(uid);
					fund2.setType(FundRecordType.Type.REPAY.index);
					fund2.setIncome(detail.getNotPaymentInterest());
					fund2.setExpend(0.0);
					fund2.setFreezen(0.0);
					fund2.setObjectId(detail.getTenderId());
					fund2.setBalance(account.getBalance()+detail.getNotPaymentInterest());			
					fund2.setRemark("收到还款(利息)");
					fund2.setTerminal("PC");
					fundList.add(fund2);
					
					account.setBalance(account.getBalance()+detail.getNotPaymentInterest());
		//				account.setTotalAmount(account.getTotalAmount()+detail.getPaymentInterest());
					//更新投资用户账户总收入
					account.setTotalIncome(account.getTotalIncome()+detail.getNotPaymentInterest());
					userAccountDao.update(account);
					
					FundRecord fund1=new FundRecord();
					fund1.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
					fund1.setCreateTime(new Date());
					fund1.setUserId(uid);
					fund1.setType(FundRecordType.Type.FEE.index);
					fund1.setIncome(0.0);
					fund1.setObjectId(detail.getTenderId());
					fund1.setExpend(detail.getFee());
					fund1.setRemark("利息管理费");
					fund1.setFreezen(0.0);
					fund1.setBalance(account.getBalance()-detail.getFee());	
					fund1.setTerminal("PC");
					fundList.add(fund1);
					
					//用户支付利息管理费
					userToPlatform(uid,detail.getFee());
					
					//更新投资用户账户可用余额
					account.setBalance(account.getBalance()-detail.getFee());
					account.setTotalAmount(account.getTotalAmount()-detail.getFee());
					//更新投资用户账户总收入
					account.setTotalIncome(account.getTotalIncome()-detail.getFee());
					userAccountDao.update(account);	
					//发送收到还款短信
					User user = userService.getById(uid);
					smsService.sendPaymentSms(user.getPhone(), BaseUtil.StringToDoubletoFixed2(detail.getNotPaymentFund()+detail.getNotPaymentInterest()-detail.getFee()), uid);
				}
				//判断是否已转让
//				Integer userId = null;
//				if (detail.getIsChange() != null && "Y".equals(detail.getIsChange())) {
//					Change change = changeDao.getChangeByTenderIdAndBorrowIdAndUserId(detail.getBorrowId(),
//							detail.getUserId(), detail.getTenderId());
//					if (change != null) {
//						userId = change.getChangeUserId();
//					}
//				} else {
//					userId = detail.getTenderId();
//				}
				UserTender tender = tenderDao.getById(detail.getTenderId());
				tender.setRecovedPeriod(tender.getRecovedPeriod() + 1);
				if (tender.getRecovedPeriod() == tender.getPeriodTotal() || borrow.getPaymentMode() == 1) {
					status = "Y";
					tender.setRepayFinish("Y");
				}
				tender.setRecovedMoney(tender.getRecovedMoney()+detail.getNotPaymentTotal());
				tender.setRecovedFund(tender.getRecovedFund()+detail.getNotPaymentFund());
				if(tender.getRecovedInterest()==null){
					tender.setRecovedInterest(0.0);
				}
				tender.setRecovedInterest(tender.getRecovedInterest()+detail.getNotPaymentInterest());
				
				tender.setRecoverInterest(tender.getRecoverInterest()-detail.getNotPaymentInterest());
				tender.setRecoverFund(tender.getRecoverFund()-detail.getNotPaymentFund());
				tender.setRecoverAll(tender.getRecoverAll()-detail.getNotPaymentTotal());
				
				tender.setAwaitTotal(tender.getAwaitTotal()-detail.getNotPaymentTotal());
				tender.setAwaitFund(tender.getAwaitFund()-detail.getNotPaymentFund());
				tender.setAwaitInterest(tender.getAwaitInterest()-detail.getNotPaymentInterest());			
				tenderList.add(tender);
				
		
				//逾期罚息(暂未设置)
				detail.setOverdueFee(0.0);
				detail.setOverdueInterest(0.0);
				//设置还款状态为【已还款】
				detail.setStatus("Y");
				//设置已还金额
				detail.setPaymentTotal(detail.getNotPaymentTotal());
				detail.setPaymentFund(detail.getNotPaymentFund());
				detail.setPaymentInterest(detail.getNotPaymentInterest());
				//设置未还金额全部为0
				detail.setNotPaymentTotal(0.0);
				detail.setNotPaymentFund(0.0);
				detail.setNotPaymentInterest(0.0);
			}
			
			//注：新手标无借款用户，属于后台发标无需减少借款用户账户金额
			UserAccount borrowUser=userAccountDao.getById(payment.getUserId());
			int borrowUserId = borrowUser.getUserId();
			borrowUser.setTotalAmount(borrowUser.getTotalAmount()-payTotal);
			borrowUser.setBalance(borrowUser.getBalance()-payTotal);
			borrowUser.setRepay(borrowUser.getRepay()-payTotal);
			borrowUser.setTotalExpenses(borrowUser.getTotalExpenses()+payTotal);
			userAccountDao.update(borrowUser);
			
			FundRecord fund=new FundRecord();
			fund.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
			fund.setCreateTime(new Date());
			fund.setUserId(borrowUserId);
			fund.setType(FundRecordType.Type.REPAY.index);
			fund.setIncome(0.0);
			fund.setExpend(payTotal);
			fund.setRemark("还款");
			fund.setFreezen(0.00);
			fund.setBalance(borrowUser.getBalance());
			fund.setTerminal("PC");
			fundList.add(fund);
			
			borrowPaymentDetailDao.batchUpdate(detailList);
			borrowPaymentDao.update(payment);
			tenderDao.batchUpdate(tenderList);
			fundRecordDao.batchInsert(fundList);
			
			
			
		
			//加息奖励
			for (BorrowPaymentDetail detail : detailList) {
				UserTender tender=tenderDao.getById(detail.getTenderId());
				Integer uid=null;
				Integer userId = null;
				if("Y".equals(detail.getIsChange())){
					Change change=changeDao.getChangeByTenderIdAndBorrowIdAndUserId(detail.getBorrowId(), detail.getUserId(), detail.getTenderId());
					if(change!=null){
						uid=change.getChangeUserId();
						userId = change.getChangeUserId();
					}
				}else{
					uid=detail.getUserId();
					userId = tender.getUserId();
				}
				
				
				UserAccount account1=userAccountDao.getByUserId(uid);
				/**
				 * 活动时间:到2016-12-31
				 */
				Date date=new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String strDate = "2016-12-31";
				Date date2 = sdf.parse(strDate);
				int num=DateUtils.betweenDays(date, date2);
				if(num>0){
					MemberLevel level=memberDao.getById(account1.getLevel());
					double rewardPercent=level.getMonthPercent();
					double rewardAmount=0.0;
					if("MONTH".equals(borrow.getDateUtil())){
						
						rewardAmount=BaseUtil.StringToDoubletoFixed2(tender.getAmount()*rewardPercent*1);
					}else{
						rewardAmount=BaseUtil.StringToDoubletoFixed2(tender.getAmount()*rewardPercent/30*borrow.getDeadline());
					}
					
					accountService.addPercent(userId, rewardAmount);		//更新资金明细		
				}	
			}
			
			borrow.setPaymentTotal(borrow.getPaymentTotal()+payTotal);
			borrow.setPaymentFund(borrow.getPaymentFund()+payFund);
			borrow.setPaymentInterest(borrow.getPaymentInterest()+payInterest);
			borrow.setNotPaymentTotal(borrow.getNotPaymentTotal()-payTotal);
			borrow.setNotPaymentFund(borrow.getNotPaymentFund()-payFund);
			borrow.setNotPaymentInterest(borrow.getNotPaymentInterest()-payInterest);
			if("Y".equals(status)){
				borrow.setStatus(BorrowStatus.COMPLETE);
			}
			borrowDao.updateBorrow(borrow);
			
			updateChangeByPament(paymentId);
			
		}
		return 1;
	}
	
	public String userToPlatform(Integer userId,Double amount) {
		User user=userDao.getById(userId);
		TransferToFreezenDto dto=new TransferToFreezenDto();
		dto.setMchnt_cd(Config.mchnt_cd);
		dto.setMchnt_txn_ssn(DateUtils.getFullFormat()+(new Random().nextInt(8999)+1000));
		dto.setOut_cust_no(user.getPhone());
		dto.setIn_cust_no(Config.user_id);
		dto.setContract_no("");
		dto.setRem("支付利息管理费");
		dto.setAmt((int)(amount*100)+"");
		String inputStr=dto.regSignVal();
		dto.setSignature(SecurityUtils.sign(inputStr));
		
		String backStr;
		try {
			backStr = WebUtils.sendHttp(Config.paltform_to_user_url,dto);
			System.out.println(backStr);
			
			Callback callback=new Callback("用户向平台转账",backStr,new Date());
			callbackDao.insert(callback);
			
			String resp_code=XmlUtils.getXmlNodeValue(backStr, "resp_code");
			String mchnt_cd=XmlUtils.getXmlNodeValue(backStr, "mchnt_cd");
			if("0000".equals(resp_code) && Config.mchnt_cd.equals(mchnt_cd)){
				System.out.println("支付利息管理费，操作成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 更新还款后债权的实时显示
	 * @param paymentId
	 * @return
	 */
	public Integer updateChangeByPament(Integer paymentId){
		BorrowPayment payment=borrowPaymentDao.getById(paymentId);
		List<BorrowPaymentDetail> detailList=borrowPaymentDetailDao.selectByPidAndIndex(payment.getId());
		for (BorrowPaymentDetail detail : detailList) {
			Change change=changeDao.getChangeByTenderIdAndBorrowIdAndUserId(detail.getBorrowId(), detail.getUserId(), detail.getTenderId());
			if(change!=null && "T".equals(change.getStatus())){
/*				double money=tenderDao.sumAwaitByNotChange(change.getTenderId());
				change.setMoney(money);
				changeDao.update(change);*/
				changeDao.delete(change.getId());
			}
		}
		return null;
	}
	
	/**
	 * 借款人还款记录
	 * @param limit
	 * @param index
	 * @param record
	 * @return
	 */
	public PageInfo<BorrowPaymentVo> list(BorrowPaymentVo vo){
		int count=	borrowPaymentDao.count(vo);
		List<BorrowPaymentVo> recordList=borrowPaymentDao.list(vo);
		SumMoneyVo sumMoneyVo = borrowPaymentDao.sumBorrowPayment(vo);
		PageInfo<BorrowPaymentVo> page=	new PageInfo<BorrowPaymentVo>(count, vo.getIndex() , vo.getLimit());
		if(sumMoneyVo == null){
			page.setSum(0.0);
			page.setSumFee(0.0);
		}else{
			page.setSum(sumMoneyVo.getFund());
			page.setSumFee(sumMoneyVo.getInterest());
		}
		page.setPageList(recordList);
		return page;
	}
	
	/**
	 * 还款详情
	 * @param id
	 * @return
	 */
	public BorrowPaymentVo detail(Integer id){
		BorrowPaymentVo vo=borrowPaymentDao.selectByPrimaryKey(id);
		return vo;
	}
	
	/**
	 * 借款标用户还款明细
	 * 注：与borrowPaymentDao.list有区别，无分页限制
	 * @param vo
	 * @return
	 */
	public List<BorrowPaymentVo> borrowUserRepayList(BorrowPaymentVo vo){
		List<BorrowPaymentVo> list = borrowPaymentDao.borrowUserRepaylist(vo);
		return list;
	}
	
	public List<BorrowPaymentVo> userPaymentList(BorrowPaymentVo vo){
		List<BorrowPaymentVo> list = borrowPaymentDao.userPaymentList(vo);
		return list;
	}
	
	/**
	 * 获取今天需要还款数据
	 * @return
	 */
	public List<BorrowPayment> listTodayPayment(String date){
		return borrowPaymentDao.listTodayPayment(date);
	}

	public BorrowPayment getById(Integer id) {
		return borrowPaymentDao.getById(id);
	}

	public List<BorrowPaymentDetail> selectByPidAndIndex(Integer id) {
		return borrowPaymentDetailDao.selectByPidAndIndex(id);
	}
	
	/**
	 * 根据借款标id获取所有还款记录
	 * @param borrowId 借款标id
	 * @return
	 */
	public List<BorrowPaymentVo> getBorrowPaymentByBorrowId(Integer borrowId){
		return borrowPaymentDao.listBorrowPaymentByBorrowId(borrowId);
	}
	
	public BorrowPayment sumPayment(Date start,Date end){
		return borrowPaymentDao.sumPayment(start,end);
	}
	
	public BorrowPayment sumNotPayment(Date start,Date end){
		return borrowPaymentDao.sumNotPayment(start,end);
	}
	
	public BorrowPayment nextMonthPayment(String date){
		return borrowPaymentDao.nextMonthPayment(date);
	}
	
	/**
	 * 根据paymentId和还款期数检查该期数之前的还款状态
	 * @param paymentId 
	 * @param index 还款当期
	 * @return
	 */
	public boolean checkPayment(Integer borrowId,Integer userId,Integer index){
		if(index==1){
			return true;
		}else{
			if(index>1){
				BorrowPaymentVo borrowPaymentVo = new BorrowPaymentVo();
				borrowPaymentVo.setUserId(userId);
				borrowPaymentVo.setBorrowId(borrowId);
				index=index-1;
				borrowPaymentVo.setPeriodIndex(index);
				List<BorrowPaymentVo> paymentList = borrowUserRepayList(borrowPaymentVo);
				
				if("Y".equals(paymentList.get(0).getStatus())){
					return true;
				}else{
					return false;
				}
			}	
		}
		return false;
	}
	
	public List<BorrowPaymentVo> exportList(BorrowRepayExportVo vo){
		return borrowPaymentDao.listByExportVo(vo);
	}
	
	public List<BorrowPaymentDetail> getDetailsByBorrow(BorrowTempVo borrowTempVo){
		return borrowPaymentDao.selectDetailsByBorrow(borrowTempVo);
	}
	
	/**
	 * 根据借款标id获取最近一次未还款记录
	 * @param borrowId 借款标id
	 * @return
	 */
	public BorrowPaymentVo getMinNotPaymentByBorrowId(Integer borrowId){
		return borrowPaymentDao.getMinNotPaymentByBorrowId(borrowId);
	}
	
	/**
	 * 根据借款标id获取最后一次已还款记录
	 * @param borrowId 借款标id
	 * @return
	 */
	public BorrowPaymentVo getMaxAlreadyPaymentByBorrowId(Integer borrowId){
		return borrowPaymentDao.getMaxAlreadyPaymentByBorrowId(borrowId);
	}
	
	public BorrowPayment paymentDetail(Integer borrowId,Integer pid){
		return borrowPaymentDao.detail(borrowId, pid);
	}
	
	public double selectDetailByBorrowIdAndTenderId(Integer borrowId,Integer tenderId){
		return borrowPaymentDao.selectDetailByBorrowIdAndTenderId(borrowId,tenderId);
	}
	
	public double getDetailByBorrowIdAndTenderId(Integer borrowId,Integer tenderId){
		return borrowPaymentDao.getDetailByBorrowIdAndTenderId(borrowId,tenderId);
	}

}